<?php
    header('content-type:text/html;charset=utf-8') ;
    @include_once('connect.php');
    // echo 'ok';



    // 接受前端传来的数据
    $uname = $_POST['username'] ;
    $id = $_POST['goods_id'] ;
    $num = $_POST['num'];
  
    // $uname = 'ww' ;
    // $id = 1;
    // $num = 2;
 


    // 返回给前端的结果
    $obj = [];

    // 判断数据是否接受成功
    if($uname && $id && $num) {
        // 判断该用户是否已经添加过了该商品
        // 如果已经添加过了  就修改数量
        // 如果没有添加    就插入数据
        $sql = "SELECT * FROM shoppingcart where username = '$uname' and good_id = $id";
        $res = mysqli_query($conn , $sql) ;
        $data = mysqli_fetch_array($res);
        if($data) {
            // 查询到了该数据，也就是直接修改数量即可
            $sql = "UPDATE shoppingcart SET num = $num  WHERE username = '$uname' and good_id = $id ";
        } 
        mysqli_query($conn , $sql) ;
        $res = mysqli_affected_rows($conn) ;
        if($res > 0) {
            $obj['status'] = true ;
            $obj['msg'] = '加入购车成功' ;
        } else {
            $obj['status'] = false ;
            $obj['msg'] = '加入失败' ;
        }

    } else {
        $obj['status'] = false ;
        $obj['msg'] = '前端数据错误' ;
    }

    echo json_encode($obj);
?>